﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace NSATBPortal
{
    public partial class TestSetEditDataSet : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string sParentName = Server.UrlDecode(Request.QueryString["ParentName"].ToString());

                lblParent.Text = "Edit Test Data Set Assignment";
                cmdSave.Text = "Save";

                linkParent.HRef += "?ID=" + Request.QueryString["ParentID"].ToString()
                                    + "&Name=" + Request.QueryString["ParentName"].ToString();

                string sVal = Request.QueryString["ID"].ToString();
                int iID = Convert.ToInt32(sVal);

                using (ATBSchemaDB objATBDB = new ATBSchemaDB())
                {
                    DataTable dtList;

                    dtList = objATBDB.GetTestDataSetList();
                    dropdownTestDataGroup.DataValueField = "ID";
                    dropdownTestDataGroup.DataTextField = "Name";
                    dropdownTestDataGroup.DataSource = dtList;
                    dropdownTestDataGroup.DataBind();
                    dropdownTestDataGroup.Items.Add(new ListItem("[--None--]", "0"));
                    dropdownTestDataGroup.SelectedValue = "0";

                    DataTable dtItem;
                    dtItem = objATBDB.GetDefTestSetDataSet(iID);
                    if(dtItem.Rows.Count > 0)
                    {
                        int iVal = 0;
                        int.TryParse(dtItem.Rows[0][0].ToString(), out iVal);
                        dropdownTestDataGroup.SelectedValue = iVal.ToString();
                    }
                }
            }
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            string sUserID = Request.LogonUserIdentity.Name;
            int iID = Convert.ToInt32(Server.UrlDecode(Request.QueryString["ID"].ToString()));

            using (ATBSchemaDB objATBDB = new ATBSchemaDB())
            {
                objATBDB.ModifyDefTestSetDataSet
                    (  
                        iID,
                        Convert.ToInt32(dropdownTestDataGroup.SelectedValue),
                        sUserID
                    );
            }

            Response.Redirect(linkParent.HRef);
            
        }

        protected void cmdCancel_Click(object sender, EventArgs e)
        {

            Response.Redirect(linkParent.HRef);

        }
    }
}